package com.zzb.framework.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 获取配置文件的工具类
 * Created by zzb on 16/1/22.
 */
public class PropertiesUtil {
    protected static Map<String, Properties> propertiesPool;//配置池
    /**通过配置文件名和key获取配置*/
    public static String getProperties(String fileName, String key) {
        if (propertiesPool == null) {
            propertiesPool = new HashMap<>();
        }
        Properties properties = null;
        if (propertiesPool.containsKey(fileName)) {
            properties = propertiesPool.get(fileName);
        } else {
            properties = new Properties();

            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            if (is == null) {
                return "";
            } else {
                try {
                    properties.load(is);
                    is.close();

                } catch (IOException e) {
                    e.printStackTrace();
                    return "";
                }
                propertiesPool.put(fileName,properties);
            }
        }
        return properties.getProperty(key,"");
    }
}
